<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- STYLESHEETS -->
    <!--[if lt IE 9]>
    <script src="../../js/flot/excanvas.min.js"></script>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
    <link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
    <link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
    <link rel="stylesheet" type="text/css" href="../../css/responsive.css">

    <link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- file-input -->
    <link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css"/>
    <!-- SELECT2 -->
    <link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css"/>
    <!-- UNIFORM -->
    <link rel="stylesheet" type="text/css" href="../../js/uniform/css/uniform.default.min.css"/>
    <!-- datatable -->
    <link rel="stylesheet" href="../../js/datatables/datatable.css">
    <!-- WIZARD -->
    <link rel="stylesheet" type="text/css" href="../../js/bootstrap-wizard/wizard.css"/>
    <link rel="stylesheet" href="../../js/datepicker/datepicker.css">
    <!-- FONTS
<link href='http://fonts.useso.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
-->
    <!-- JQUERY -->
    <script src="../../js/jquery/jquery-2.0.3.min.js"></script>
    <script src="../../js/common.js"></script>
</head>

<body>
<header class="navbar clearfix" id="header">
</header>

<!-- PAGE -->
<section id="page">
    <!-- SIDEBAR -->
    <div id="sidebar" class="sidebar">
        <div class="sidebar-menu nav-collapse">

            <!-- SIDEBAR MENU -->
            <ul>

            </ul>
            <!-- /SIDEBAR MENU -->
        </div>

    </div>
    <!-- /SIDEBAR -->
    <div id="main-content">
        <div class="modal fade" id="productpack_detail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title">选择商品</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row" style="height:auto">
                            <label class="control-label col-md-3">产品名称：</label>
                            <div class="col-md-8 inline">
                                <input type="text" class="form-control width-50" name="q"   placeholder="请输入产品名称" />
                                <a class="btn btn-primary searchProduct">查询</a>
                            </div>

                        </div>
                        <div class="form-group" style="height:auto">
                            <table id="productpack_product" cellpadding="0" cellspacing="0" border="0" class="datatable table table-striped table-bordered table-hover">


                            </table>
                        </div>



                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

                    </div>
                </div>
            </div>
        </div>
        <!-- /SAMPLE BOX CONFIGURATION MODAL FORM-->
        <div class="container">
            <div class="row">
                <div id="content" class="col-lg-12">
                    <!-- PAGE HEADER-->
                    <div class="row">
                        <div class="col-sm-12">
                            <div class="page-header">
                                <!-- STYLER -->

                                <!-- /STYLER -->
                                <!-- BREADCRUMBS -->
                                <ul class="breadcrumb">
                                    <li>
                                        <i class="fa fa-home"></i>
                                        <a href="index.html">首页</a>
                                    </li>
                                    <li>
                                        <a href="#">商品管理</a>
                                    </li>
                                    <li>维护商品</li>
                                </ul>
                                <!-- /BREADCRUMBS -->

                            </div>
                        </div>
                    </div>
                    <!-- /PAGE HEADER -->
                    <!-- SAMPLE -->
                    <form id="form_pack">
                        <div class="row col-md-12">
                            <div class="box border primary" style="margin-left: 20px;">
                                <div class="box-title">
                                    <h4><i class="fa fa-bars"></i>砍价活动设置</h4>
                                </div>
                                <div class="box-body big" style="padding:10px;height:auto">
                                    <div class="row " style="margin-top:20px">
                                        <label class="control-label col-md-3"><span class="required">*</span>销售平台：</label>
                                        <div class="col-md-8" style="text-align: left">
                                            <select id="platform_select" class="col-md-6 select2-offscreen" tabindex="-1">
                                                <option value="All">全部</option>
                                                <option value="M000">壹果</option>
                                                <option value="M001">壹家店</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row" style="margin-top:10px;">
                                        <label class="control-label col-md-3"><span class="required">*</span>活动名称：</label>
                                        <div class="col-md-8 inline">
                                            <input type="text" class="form-control width-50" name="activeTitle" placeholder="请输入活动名称" maxlength="50"/>
                                            <input type="hidden" name="id"/>
                                            <span class="error-span"></span>
                                        </div>
                                    </div>
                                    <div class="row" id="rangDate">
                                        <label class="control-label col-md-3"><span class="required">*</span>生效日期：</label>
                                       <div class="col-md-6 inline" style="display:inline-flex">
												<input class="Wdate" style="height:34px" type="text" id="d4311" name="startTime" onfocus="WdatePicker({maxDate:'#F{$dp.$D(\'d4312\')||\'2030-10-01\'}',dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false,readOnly:true})"> -
												<input class="Wdate" type="text" style="height:34px" id="d4312" name="endTime" onfocus="WdatePicker({minDate:'#F{$dp.$D(\'d4311\')}',maxDate:'2030-10-01',dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false,readOnly:true})">
												<span class="error-span"></span>
									   </div>
                                    </div>
                                    <div class="row">
                                        <label class="control-label col-md-3"><span class="required">*</span>倒计时（小时）：</label>
                                        <div class="col-md-8 inline">
                                            <textarea name="countDown" class="form-control" style="width:320px;resize:none; height:34px"></textarea>
                                            <span class="error-span"></span>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <label class="control-label col-md-3"><span class="required">*</span>目标价：</label>
                                        <div class="col-md-8 inline">
                                            <input type="text" class="form-control width-50" name="targetPrice" onkeypress="return $.IsDecimal(event);"
                                                   onblur="return $.IsMoney(this);" placeholder="请输入目标价" maxlength="10"/>

                                            <span class="error-span"></span>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="control-label col-md-3"><span class="required">*</span>生成方式：</label>
                                        <div class="col-md-8 " style="display: flex">
                                            <input type="radio"  class='Order' checked="checked" name="generationMode" value="Order" style="margin-top:10px"><span style='margin-left:10px;line-height: 34px'>加运费</span><input type="text" class="form-control" style="width: 150px" name="expressPrice" value="0" >
                                            <input type="radio" class="CardCoupon" name="generationMode" value="CardCoupon" style="margin-top:10px"><span style='margin-left:10px;line-height: 34px'>卡券</span><input type="text" class="form-control" style="width: 150px" name="cardCountDown"
                                                                                                                                                                                                                     placeholder="请输入卡券顺延有效天数">天

                                            <span class="error-span"></span>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="control-label col-md-3"><span class="required">*</span>每人限购：</label>
                                        <div class="col-md-8 inline">
                                            <input type="text" class="form-control width-50" name="buyCount" onkeypress="return $.IsDecimal(event);"
                                                   onblur="return $.IsNum(this);" onfocus="this.select()" onmouseover="this.onfocus()" placeholder="请输入限购单数,0不限购"
                                                   maxlength="10"/>
                                            <span class="error-span"></span>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="control-label col-md-3"><span class="required">*</span>仅限新用户帮砍：</label>
                                        <div class="col-md-8 inline">
                                            <input type="checkbox" name="cutOnlyNew">选中,表示仅限新用户帮砍
                                            <span class="error-span"></span>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="control-label col-md-3"><span class="required">*</span>适用商品：</label>

                                            <div class="col-sm-1">
                                                <a class="btn btn-primary search">查询</a>
                                            </div>

                                    </div>
                                    <div class="row" style="margin-left: 0px;margin-right:0px">
                                        <div class="box border orange">
                                            <div class="box-title" style="height: 34px">
                                                <h6 style="margin-top: 4px;">已选择商品</h6>
                                            </div>
                                            <div class="box-body">
                                                <table id="td_selectproduct" cellpadding="0" cellspacing="0" border="0"
                                                       class="datatable table table-striped table-bordered table-hover">
                                                    <thead>
                                                    <tr>
                                                        <th class="col-md-3">商品名称</th>
                                                        <th class="col-md-1">最小砍价人数</th>
                                                        <th class="col-md-1">最大砍价人数</th>
                                                        <th class="col-md-1">可砍数量</th>
                                                        <th class="col-md-3"> 是否真实库存</th>
                                                        <th class="col-md-1">操作</th>


                                                    </tr>
                                                    </thead>
                                                    <tbody>
                                                    <tr class="empty">
                                                        <td colspan=6>请从左侧添加商品</td>
                                                    </tr>
                                                    </tbody>

                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                    <div class="row" style="height: 30px;">
                        <div class="col-md-12" style="text-align: center;">
                            <div class="box ">
                                <a href="#" class="btn btn-success config bt-submit" style="width: 100px">
                                    保存
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>


            <!-- /SAMPLE -->

            <div class="footer-tools">
								<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
            </div>
        </div>
        <!-- /CONTENT-->
    </div>
    </div>
    </div>
</section>
<!--/PAGE -->
<!-- JAVASCRIPTS -->
<!-- Placed at the end of the document so the pages load faster -->

<!-- BOOTSTRAP -->
<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>

<!-- BLOCK UI -->
<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
<!--bootbox-->
<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
<!-- AUTOSIZE -->
<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
<!-- spinner -->
<script src="../../js/spinner/spin.js"></script>
<!-- INPUT MASK -->

<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>
<!-- UNIFORM -->
<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
<!-- DATA TABLES -->
<script type="text/javascript" src="../../js/datatables/bootstrap-select.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable-bootstrap.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable-tabletools.js"></script>


<!-- SELECT2 -->
<script type="text/javascript" src="../../js/select2/select2.min.js"></script>

  <script language="javascript" type="text/javascript" src="../../js/My97DatePicker/WdatePicker.js"></script>
<script src="../../js/jquery-validate/jquery.validate.min.js"></script>
<!-- COOKIE -->
<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
<!-- CUSTOM SCRIPT -->
<script src="../../js/script.js"></script>

<script src="../../js/Sortable.min.js"></script>
<script>
    var editBargain = function () {
        var wizform = $('#form_pack');

        var id = null, flag;

        var formValiInit = function () {
            wizform.validate({
                doNotHideMessage: true,
                errorClass: 'error-span',
                errorElement: 'span',
                rules: {
                    activeTitle: {
                        required: true
                    },
                    targetPrice: {
                        required: true
                    },
                    cutCount: {
                        required: true
                    },
                    countDown: {
                        required: true
                    }
                },
                highlight: function (element) {
                    $(element)
                        .closest('.form-group').removeClass('has-success').addClass('has-error');
                },

                unhighlight: function (element) {
                    $(element)
                        .closest('.form-group').removeClass('has-error');
                },
                success: function (label) {
                    label.addClass('valid')
                        .closest('.form-group').removeClass('has-error').addClass('has-success');

                }
            });
        };

        var save = function () {
            if (wizform.valid() == false) return false;
            var editBargainReq = {};
            editBargainReq.id = id;
            editBargainReq.activeName = $("input[name=activeTitle]").val();
            editBargainReq.targetPrice = $("input[name=targetPrice]").val();
            editBargainReq.cutCount = $("input[name=cutCount]").val();
            editBargainReq.countDown = $("[name=countDown]").val();
            editBargainReq.startDate = new Date($("input[name=startTime]").val());
            editBargainReq.endDate = new Date($("input[name=endTime]").val());
            editBargainReq.platformNo=$("#platform_select").val();
            editBargainReq.buyCount=$("[name=buyCount]").val();
            editBargainReq.generationMode=$("[name=generationMode]:checked").val();
            if (editBargainReq.generationMode == "Order")
                editBargainReq.cardCountDown = 0;
            else
                editBargainReq.cardCountDown = $("[name=buyCount]").val();
            editBargainReq.expressPrice=$("[name=expressPrice]").val();
            editBargainReq.cutOnlyNew=$("[name=cutOnlyNew]").prop("checked");

            editBargainReq.detailList=[];
            $("#td_selectproduct tbody tr").each(function () {
                var p={};
                p.productId=$(this).find("td:eq(0)").attr("productId");
                p.minCutCount=$(this).find("td:eq(1)").find("input").val();
                p.maxCutCount = $(this).find("td:eq(2)").find("input").val();
                p.stockQuantity=$(this).find("td:eq(3)").find("input").val();
                p.realStock = $(this).find("td:eq(4)").find("input").prop("checked");
                editBargainReq.detailList.push(p);

            })

            if (editBargainReq.targetPrice < 0) {
                $.alert("请设置目标价");
                return false;
            }
            if (editBargainReq.cutCount == 0) {
                $.alert("请设置帮砍人数");
                return false;
            }
            $.blockUI();
            $.confirm("确认数据都已经维护",function(){
                $.ajax({
                    type: "post",
                    url: App.getContextPath() + "admin/bargain/edit.do",
                    dataType: "json",
                    contentType: "application/json",
                    data: JSON.stringify(editBargainReq),
                    async: true,
                    success: function (result) {
                        $.unblockUI();
                        if (result.type == "error") {
                            $.alert(result.content);

                        } else {
                            location.href = "list.html";
                        }
                    }
                })
            }, function () {
                $.unblockUI();
            })
         ;
        };
        var searchProduct = function () {

            var url = App.getContextPath() + "admin/bargain/findProductForBargain.do?productName=" + $("input[name=q]").val();
            $.ajax({
                type: "post",
                url: url,
                async: true,
                success: function (result) {
                    if (result.extra != null)
                        $("#productpack_product").empty();
                    $.each(result.extra, function (index, item) {
                        var tr = '<tr >' +
                            '<td class="productName">' + item.productName + '</td>' +
                            '<td><a class="btn btn-warning btn-xs bt-join btn-width"  pstatus="join" productName="'+item.productName+'" id="'+item.id+'">' +
                            '<i class="fa fa-plus-square-o" style="margin-right:5px"></i>参与活动</a>' +
                            '</td><td class="productCode hide">' + item.id + '</td></tr>';
                        $("#productpack_product").append(tr);
                        $("#productpack_product").find(".bt-join").unbind("click").bind("click",function () {
                            $(this).closest("tr").remove();
                            var item={}
                            item.productName=$(this).attr("productName");
                            item.productId=$(this).attr("id");
                            item.minCutCount=1;
                            item.maxCutCount=1;
                            item.stockQuantity=1;
                            item.realStock="checked"
                            addProduct(item)
                        })
                    })

                }
            });
        };
        var addProduct=function(item){

            var tr = '<tr>' +
                '<td class="productName" productId="'+item.productId+'">' + item.productName + '</td>' +
                '<td ><input type="text" class="form-control col-md-2" name="minCutCount" value="'+item.minCutCount+'"/></td>' +
                '<td ><input type="text" class="form-control col-md-2" name="maxCutCount" value="'+item.maxCutCount+'"/></td>' +
                '<td ><input type="text" class="form-control col-md-2" name="stockQuantity"  value="'+item.stockQuantity+'"/></td>' +
                '<td ><input type="checkbox"  name="realStock" checked="'+item.realStock+'"/>选中,库存不足时系统自动取消进行中的砍价</td>' +
                '<td><a class="btn btn-warning btn-xs bt-del btn-width" >' +
                '<i class="fa fa-plus-square-o" style="margin-right:5px"></i>删除</a>' +
                '</td></tr>';
            if(!$("#td_selectproduct tbody>tr>td:eq(0)").attr("productId")){
                $("#td_selectproduct tbody").empty();
            }
            $("#td_selectproduct tbody").append(tr);
            $("#td_selectproduct tbody").find(".bt-del").unbind("click").bind("click",function(){
                    $(this).closest("tr").remove();
            });
        }
        var getBargainProduct = function (id) {
            $.ajax({
                type: "post",
                url: App.getContextPath() + "admin/bargain/findDetailById.do?id=" + id,
                async: true,
                success: function (result) {
                    if (result != null)
                        $("#td_selectproduct tbody").empty();
                    $.each(result.extra, function (index, item) {
                        addProduct(item);
                    })
                }
            });


        }


        //获取活动详情
        var getBargain = function (id) {
            $.ajax({
                type: "post",
                url: App.getContextPath() + "admin/bargain/findById.do",
                data: {id: id},
                async: true,
                success: function (result) {
                    result = result.extra;
                    $("[name=activeTitle]").val(result.activeName);
                    $("[name=targetPrice]").val(result.targetPrice);
                    $("[name=cutCount]").val(result.cutCount);
                    $("[name=countDown]").val(result.countDown);
                    $("[name=startTime]").val(new Date(parseInt(result.startDate)).format("yyyy-MM-dd hh:mm:ss"));
                    $("[name=endTime]").val(new Date(parseInt(result.endDate)).format("yyyy-MM-dd hh:mm:ss"));
                    $("#platform_select").select2("val",result.platformNo);
                    $("[name=buyCount]").val(result.buyCount);
                    $("." + result.generationMode).prop('checked', true)
                    $("[name=expressPrice]").val(result.expressPrice);
                    $("[name=cardCountDown]").val(result.cardCountDown);
                    $("[name=cutOnlyNew]").prop("checked",result.cutOnlyNew);
                    getBargainProduct(result.id);

                }
            });
        };


        return {
            init: function () {
                App.init();
                formValiInit();
                $("#platform_select").select2().select2("val","All");

                var date=new Date();
				$("[name=startTime]").val(date.format("yyyy-MM-dd hh:mm:ss"));
				$("[name=endTime]").val(date.format("yyyy-MM-dd hh:mm:ss"));
             
                $(".bt-submit").click(save);
                flag = $.getUrlParam("flag");
                id = $.getUrlParam("id");
                if (id != null) {
                    getBargain(id);
                }
                $(".search").click(function () {

                 $("#productpack_detail").modal("show");
                    $("#productpack_detail").find(".searchProduct").click(searchProduct)

                })


            }
        }
    }();
    $(function () {
        $("#header").load("../head.html");
        editBargain.init();
    });
</script>

<!-- /JAVASCRIPTS -->
</body>

</html>